package org.wang.generalcode.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.pojo.entity.UserRole;
import org.wang.generalcode.modules.sys.service.IUserRoleService;
import org.wang.generalcode.modules.sys.pojo.vo.UserRoleVO;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;

/**
 * 用户和角色关联表 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/useRole")
@Schema(description = "用户和角色关联表", name = "用户和角色关联表接口")
public class UserRoleController {

	private final IUserRoleService userRoleService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Schema(description = "详情", example = "传入userRole")
	public Result<UserRole> detail(UserRole userRole) {
		UserRole detail = userRoleService.getOne(Condition.getQueryWrapper(userRole));
		return Result.success(detail);
	}

	/**
	 * 分页 用户和角色关联表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Schema(description = "分页", example = "传入userRole")
	public Result<IPage<UserRole>> list(UserRole userRole, Query query) {
		IPage<UserRole> pages = userRoleService.page(Condition.getPage(query), Condition.getQueryWrapper(userRole));
		return Result.success(pages);
	}

	/**
	 * 自定义分页 用户和角色关联表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Schema(description = "分页", example = "传入userRole")
	public Result<IPage<UserRoleVO>> page(UserRoleVO userRole, Query query) {
		IPage<UserRoleVO> pages = userRoleService.selectUserRolePage(Condition.getPage(query), userRole);
		return Result.success(pages);
	}

	/**
	 * 新增 用户和角色关联表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Schema(description = "新增", example = "传入userRole")
	public Result save(@Valid @RequestBody UserRole userRole) {
		return Result.judge(userRoleService.save(userRole));
	}

	/**
	 * 修改 用户和角色关联表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Schema(description = "修改", example = "传入userRole")
	public Result update(@Valid @RequestBody UserRole userRole) {
		return Result.judge(userRoleService.updateById(userRole));
	}

	/**
	 * 新增或修改 用户和角色关联表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Schema(description = "新增或修改", example = "传入userRole")
	public Result submit(@Valid @RequestBody UserRole userRole) {
		return Result.judge(userRoleService.saveOrUpdate(userRole));
	}


	/**
	 * 删除 用户和角色关联表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Schema(description = "删除", example = "传入ids")
	public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return Result.judge(userRoleService.removeByIds(Func.toLongList(ids)));
	}


}
